package com.zzh.takeoutapp.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import com.zzh.takeoutapp.R;

import java.util.List;

/**
 * Created by Administrator on 2018/7/25.
 */

public class DiscountHeaderBannerAdapter extends PagerAdapter {

    private List<ImageView> ivList; // ImageView的集合
    private int count; // 广告的数量
    private LayoutInflater inflater;
    private Context mContext;

    public DiscountHeaderBannerAdapter(List<ImageView> ivList, Context context) {
        super();
        this.ivList = ivList;
        this.mContext = context;
        this.inflater = LayoutInflater.from(mContext);
        if (ivList != null) {
            count = ivList.size();
        }
    }


    @Override
    public int getCount() {
        if (ivList == null) return 0;
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = inflater.inflate(R.layout.itme_discount_card_view, container, false);
        //如果View已经在之前添加到了一个父组件，则必须先remove，否则会抛出IllegalStateException。
        ViewParent vp = view.getParent();
        if (vp != null) {
            ViewGroup parent = (ViewGroup) vp;
            parent.removeView(view);
        }
        container.addView(view);
        return view;
    }
}